package com.example.yilanchatserver.business.freechat.service.impl;

import com.example.yilanchatserver.business.freechat.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @program: RoleServiceImpl
 * @description:
 * @author: bob
 * @create: 3/24/21 5:33 PM
 */
@Component
public class RedisServiceImpl implements RedisService {

    @Autowired
    @Qualifier("selectableRedisTemplate")
    private SelectableRedisTemplate stringRedisTemplate;

    @Override
    public void set(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    @Override
    public String get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    @Override
    public boolean expire(String key, long expire) {
        return stringRedisTemplate.expire(key, expire, TimeUnit.MINUTES);
    }

    /**
     * 缓存基本的对象，Integer、String、实体类等
     *
     * @param key 缓存的键值
     * @param value 缓存的值
     */
    public void set(String key, String value, int expire, TimeUnit timeUnit) {
        stringRedisTemplate.opsForValue().set(key, value, expire, timeUnit);
//        System.out.println("success");
    }

    @Override
    public void remove(String key) {
        stringRedisTemplate.delete(key);
    }

    @Override
    public Long increment(String key, long delta) {
        return stringRedisTemplate.opsForValue().increment(key,delta);
    }

    @Override
    public void rpush(String key, List<String> content) {
        stringRedisTemplate.opsForList().rightPushAll(key, content);
        stringRedisTemplate.expire(key, 3600, TimeUnit.SECONDS);
    }

    @Override
    public void rpush(String key, String content) {
        stringRedisTemplate.opsForList().rightPush(key, content);
        stringRedisTemplate.expire(key, 3600, TimeUnit.SECONDS);
    }

    @Override
    public List<String> range(String key) {
        return stringRedisTemplate.opsForList().range(key, 0, -1);
    }

    @Override
    public Boolean hasKey(String key) {
        return stringRedisTemplate.hasKey(key);
    }

    @Override
    public void rpop(String key) {
        stringRedisTemplate.opsForList().rightPop(key);
    }

    @Override
    public void deleteElement(String key, String value) {
        stringRedisTemplate.opsForList().remove(key, 1, value);
    }
}
